﻿/**************************************************************************
创建时间:	2020/5/14 14:28:19    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) 河北朝仁医药科技有限公司

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.Controls;

namespace ZhCun.Win.ExtendGridView
{
    static class FormGridViewEx
    {
        private static readonly GridViewBLL DBConfig;

        static FormGridViewEx()
        {
            //_Config = SpringHelper.GetObject<IGridViewConfig>("Common.BLL.GridViewConfig");
            DBConfig = new GridViewBLL();
        }
        /// <summary>
        /// 创建一个GridView的文本框列
        /// </summary>
        /// <param name="text">标题</param>
        /// <param name="pName">数据源名称</param>
        /// <returns></returns>
        private static DataGridViewTextBoxColumn CreateTextBox(string text, string pName)
        {
            return new DataGridViewTextBoxColumn { HeaderText = text, DataPropertyName = pName };
        }
        /// <summary>
        /// 创建一个GridView的CheckBox
        /// </summary>
        private static DataGridViewColumn CreateCheckBox(string text, string pName)
        {
            return new DataGridViewCheckBoxColumn { HeaderText = text, DataPropertyName = pName };
        }
        /// <summary>
        /// 初始化GridView列
        /// </summary>
        private static void InitGridColumn(DataGridView dgv, List<Sys_GridColumn> cols)
        {
            if (cols.Exists(s => s.IsReadOnly == false))
            {
                dgv.ReadOnly = false;
            }
            if (cols.Count == 1)
            {
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            else if (cols.Exists(s => s.ColumnWidth > 0))
            {
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            }

            foreach (var itemCol in cols)
            {
                if (!itemCol.IsVisible) continue;

                if (!itemCol.DgvName.StringEquals(dgv.Name))
                {
                    continue;
                }

                DataGridViewColumn dgvCol;
                EnumHelper.StringToEnum(itemCol.ColumnType, out DgvColumnType colType);

                switch (colType)
                {
                    case DgvColumnType.CheckBox:
                        dgvCol = CreateCheckBox(itemCol.ColumnTitle, itemCol.ColumnName);
                        break;
                    case DgvColumnType.TextBox:
                    default:
                        dgvCol = CreateTextBox(itemCol.ColumnTitle, itemCol.ColumnName);
                        break;
                }

                if (EnumHelper.StringToEnum(itemCol.Alignment, out DataGridViewContentAlignment alignment))
                {
                    if (alignment != DataGridViewContentAlignment.NotSet)
                        dgvCol.DefaultCellStyle.Alignment = alignment;
                }
                if (EnumHelper.StringToEnum(itemCol.AutoSizeMode, out DataGridViewAutoSizeColumnMode colAutoSizeMode))
                {
                    if (colAutoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
                    {
                        dgvCol.AutoSizeMode = colAutoSizeMode;
                    }
                }

                if (itemCol.ColumnWidth > 0)
                    dgvCol.Width = itemCol.ColumnWidth;

                if (!itemCol.FormatStr.IsEmpty())
                    dgvCol.DefaultCellStyle.Format = itemCol.FormatStr;
                dgvCol.Name = itemCol.ColumnName;
                dgvCol.ReadOnly = itemCol.IsReadOnly;
                dgv.Columns.Add(dgvCol);
            }
        }
        /// <summary>
        /// 初始化gridview,可以同时初始化多个
        /// </summary>
        public static void InitGridView(this IFormContainer form, params DataGridView[] dgvs)
        {
            var frmName = form.ContainerName;
            var cols = DBConfig.GetColumnInfo(frmName);
            if (cols == null || cols.Count == 0) return;
            if (dgvs == null || dgvs.Length == 0)
            {
                var dgvNames = cols.DistinctBy(s => s.DgvName).Select(s => s.DgvName).ToArray();
                dgvs = new DataGridView[dgvNames.Length];
                for (int i = 0; i < dgvNames.Length; i++)
                {
                    var dgvCtls = form.Controls.Find(dgvNames[i], true);
                    if (dgvCtls != null && dgvCtls.Length > 0)
                    {
                        if (dgvCtls[0] is DataGridView dgv)
                        {
                            dgvs[i] = dgv;
                        }
                    }
                }
            }

            foreach (DataGridView dgv in dgvs)
            {
                if (dgv == null) continue;
                dgv.Columns.Clear();
                dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                var dgvCols = cols.FindAll(s => s.DgvName == dgv.Name);

                InitGridColumn(dgv, dgvCols);
            }
        }
    }
}